package com.mycontract.suitepro.utils

import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences
import com.mycontract.suitepro.ProjectApplication
import com.mycontract.suitepro.ProjectConfig
import com.mycontract.suitepro.data.*
import com.mycontract.suitepro.extend.*


/** 本地缓存工具 */
object SpUtil {

    //sp key token
    val SP_CONTRACTSUITE_KEY_CONTRACTSUITE_TOKEN = "TOKEN"

    //sp key user id
    val SP_CONTRACTSUITE_KEY_CONTRACTSUITE_USER_CONTRACTSUITE_ID = "USER_ID"

    //sp key mobile
    val SP_CONTRACTSUITE_KEY_CONTRACTSUITE_MOBILE = "MOBILE"

    //sp key country
    val SP_CONTRACTSUITE_KEY_CONTRACTSUITE_COUNTRY = "COUNTRY"

    //sp key api path
    val SP_CONTRACTSUITE_KEY_CONTRACTSUITE_API_CONTRACTSUITE_PATH = "APIPATH"

    //sp key app review active
    val SP_CONTRACTSUITE_KEY_CONTRACTSUITE_APP_CONTRACTSUITE_REVIEW_CONTRACTSUITE_ACTIVE = "APPREVIEWACTIVE"

    //sp key app review normal
    val SP_CONTRACTSUITE_KEY_CONTRACTSUITE_APP_CONTRACTSUITE_REVIEW_CONTRACTSUITE_NORMAL = "APPREVIEWNORMAL"

    //sp key ad source
    val SP_CONTRACTSUITE_KEY_CONTRACTSUITE_IS_CONTRACTSUITE_AD_CONTRACTSUITE_SOURCE = "ISADSOURCE"
    
    /** 获取sp句柄 */
    fun getContractSuiteSp(): SharedPreferences {
        val sp = ProjectApplication.context.getSharedPreferences(
            ProjectConfig.CONTRACTSUITE_Product,
            Context.MODE_PRIVATE
        )
        return sp
    }

    @SuppressLint("ApplySharedPref")
    fun saveContractSuiteSpContractSuiteValue(strKey: String, strValue: String) {
        val sp = getContractSuiteSp().edit()
        sp.putString(strKey, strValue)
        sp.commit()
    }


    @SuppressLint("ApplySharedPref")
    fun saveContractSuiteSpContractSuiteValueContractSuiteInt(strKey: String, intValue: Int) {
        val sp = getContractSuiteSp().edit()
        sp.putInt(strKey, intValue)
        sp.commit()
    }

    /** 保存 token */
    @SuppressLint("ApplySharedPref")
    fun saveContractSuiteToken(gToken: String) {
        saveContractSuiteSpContractSuiteValue(SP_CONTRACTSUITE_KEY_CONTRACTSUITE_TOKEN, gToken)
    }

    /** 获取 token */
    @SuppressLint("ApplySharedPref")
    fun getContractSuiteToken(): String {
        return getContractSuiteSp().getString(SP_CONTRACTSUITE_KEY_CONTRACTSUITE_TOKEN, "").toString()
    }

    /** 保存 user id */
    @SuppressLint("ApplySharedPref")
    fun saveContractSuiteUserContractSuiteId(gUserId: String) {
        saveContractSuiteSpContractSuiteValue(SP_CONTRACTSUITE_KEY_CONTRACTSUITE_USER_CONTRACTSUITE_ID, gUserId)
    }

    /** 获取 user id */
    @SuppressLint("ApplySharedPref")
    fun getContractSuiteUserContractSuiteId(): String {
        return getContractSuiteSp().getString(SP_CONTRACTSUITE_KEY_CONTRACTSUITE_USER_CONTRACTSUITE_ID, "")
            .toString()
    }

    /** 保存 mobile */
    @SuppressLint("ApplySharedPref")
    fun saveContractSuiteMobile(gMobile: String) {
        saveContractSuiteSpContractSuiteValue(SP_CONTRACTSUITE_KEY_CONTRACTSUITE_MOBILE, gMobile)
    }

    /** 获取 mobile */
    @SuppressLint("ApplySharedPref")
    fun getContractSuiteMobile(): String {
        return getContractSuiteSp().getString(SP_CONTRACTSUITE_KEY_CONTRACTSUITE_MOBILE, "")
            .toString()
    }

    /** 保存 country */
    @SuppressLint("ApplySharedPref")
    fun saveContractSuiteCountry(gCountry: String) {
        saveContractSuiteSpContractSuiteValue(
            SP_CONTRACTSUITE_KEY_CONTRACTSUITE_COUNTRY,
            gCountry
        )
    }

    /** 获取 country */
    @SuppressLint("ApplySharedPref")
    fun getContractSuiteCountry(): String {
        return getContractSuiteSp().getString(SP_CONTRACTSUITE_KEY_CONTRACTSUITE_COUNTRY, "")
            .toString()
    }

    /** 保存 api path */
    @SuppressLint("ApplySharedPref")
    fun saveContractSuiteApiContractSuitePath(gApiPath: String) {
        saveContractSuiteSpContractSuiteValue(
            SP_CONTRACTSUITE_KEY_CONTRACTSUITE_API_CONTRACTSUITE_PATH,
            gApiPath
        )
    }

    /** 获取 api path */
    @SuppressLint("ApplySharedPref")
    fun getContractSuiteApiContractSuitePath(): String {
        return getContractSuiteSp().getString(SP_CONTRACTSUITE_KEY_CONTRACTSUITE_API_CONTRACTSUITE_PATH, "")
            .toString()
    }

    /** 保存 ad source */
    @SuppressLint("ApplySharedPref")
    fun saveContractSuiteAdContractSuiteSource(gAdSource: String) {
        saveContractSuiteSpContractSuiteValue(
            SP_CONTRACTSUITE_KEY_CONTRACTSUITE_IS_CONTRACTSUITE_AD_CONTRACTSUITE_SOURCE,
            gAdSource
        )
    }

    /** 获取 ad source */
    @SuppressLint("ApplySharedPref")
    fun getContractSuiteAdContractSuiteSource(): String {
        return getContractSuiteSp().getString(SP_CONTRACTSUITE_KEY_CONTRACTSUITE_IS_CONTRACTSUITE_AD_CONTRACTSUITE_SOURCE, "")
            .toString()
    }

    /** 保存 app review active */
    @SuppressLint("ApplySharedPref")
    fun saveContractSuiteAppContractSuiteReviewContractSuiteActive(intValue: Int) {
        saveContractSuiteSpContractSuiteValueContractSuiteInt(
            SP_CONTRACTSUITE_KEY_CONTRACTSUITE_APP_CONTRACTSUITE_REVIEW_CONTRACTSUITE_ACTIVE,
            intValue
        )
    }

    /** 获取 app review active */
    @SuppressLint("ApplySharedPref")
    fun getContractSuiteAppContractSuiteReviewContractSuiteActive(): Int {
        return getContractSuiteSp().getInt(SP_CONTRACTSUITE_KEY_CONTRACTSUITE_APP_CONTRACTSUITE_REVIEW_CONTRACTSUITE_ACTIVE, 0)
    }

    /** 保存 app review normal */
    @SuppressLint("ApplySharedPref")
    fun saveContractSuiteAppContractSuiteReviewContractSuiteNormal(intValue: Int) {
        saveContractSuiteSpContractSuiteValueContractSuiteInt(
            SP_CONTRACTSUITE_KEY_CONTRACTSUITE_APP_CONTRACTSUITE_REVIEW_CONTRACTSUITE_NORMAL,
            intValue
        )
    }

    /** 获取 app review normal */
    @SuppressLint("ApplySharedPref")
    fun getContractSuiteAppContractSuiteReviewContractSuiteNormal(): Int {
        return getContractSuiteSp().getInt(SP_CONTRACTSUITE_KEY_CONTRACTSUITE_APP_CONTRACTSUITE_REVIEW_CONTRACTSUITE_NORMAL, 0)
    }
}